<?php
/**
* Do all the declarations etc needed by an admin page
*/
include_once "adminheader.inc";

echo "Adding coupon to database.<br />";

$cleanPost = cleanInput($_POST);

// remove $ and , from money amounts, this is US specific
$cleanPost['fixed_cost'] = str_replace(array('$',','),'',$cleanPost['fixed_cost']);

// remove %
$cleanPost['discount'] = str_replace(array('%',','),'',$cleanPost['discount']);

// add the cleaned data to the database
$db_insert = 'INSERT INTO ' . $xoopsDB->prefix('confreg_coupons') .
				' SET code=' . $xoopsDB->quoteString(substr($cleanPost['code'],0,10)) .
				', description=' . $xoopsDB->quoteString(substr($cleanPost['desc'],0,128)) .
				', fixed_cost=' . floatval($cleanPost['fixed_cost']) .
				', discount=' . floatval($cleanPost['discount']) .
				', conf_id=' . intval($cleanPost['conf_id']);

echo "Query built.<br />";

$insert_res = $xoopsDB->query($db_insert);

if (!$insert_res) {
	echo "<p>Error occured inserting extra!";
	// debug info
	echo "<p>Error Number:" . $xoopsDB->errno() . "<br \>";
	echo "<p>Error:" . $xoopsDB->error() . "<p>";
//	echo '<p><code style="text-align:left;>' . $db_insert . "</code>";
//	echo '<p><code style="text-align:left;>' . var_dump($cleanPost) . "</code>";
} else {
	$insert_id = $xoopsDB->getInsertId();
	echo 'Coupon ' . $cleanPost['title'] . ' has been created. Coupon id is ' . $insert_id . '<br />';
	echo '<p><form method="POST" action="get_coupon_data.php"><input type="hidden" name="conf_id" value="' . intval($cleanPost['conf_id']) . '" />';
	echo '<input type="submit" value="'. _AM_CONFREG_COUPON_ADD_ANOTHER . '" /></form> <p> <a href="admenu0.php">Back to conference setup</a><p>';
}

xoops_cp_footer();

/*
 * This file was generated by XBS ModGen, (c) 2006 A Kitson, UK. See http://xoobs.net
 * ModGen is a Module Code Generator for the Xoops CMS.  See http://xoops.org
 */
?>
